﻿using System.Linq;
using CarFileManager.Models;

namespace CarFileManager
{
    public abstract class CarFileManagerBase
    {
        private readonly CarFileBase _carFile;
        
        private int _index = -1;

        public string FileName
        {
            get { return _carFile.FileName; }
        }

        public ICar CurrentCar
        {
            get { return _carFile.Cars.ElementAtOrDefault(_index); }
        }

        protected CarFileManagerBase(CarFileBase carFile)
        {
            _carFile = carFile;
            _carFile.ReadFile();
            First();
        }
        
        public void Add(ICar car)
        {
            _carFile.Cars.Add(car);
            Last();
        }

        public void Save()
        {
            _carFile.SaveFile();
        }

        public void Delete()
        {
            _carFile.Cars.RemoveAt(_index);
            Previous();
        }

        public void First()
        {
            _index = _carFile.Cars.Count == 0 ? -1 : 0;
        }

        public void Previous()
        {
            if (_index > _carFile.Cars.Count - 1)
            {
                _index--;
            }
        }

        public void Next()
        {
            if (_index < _carFile.Cars.Count - 1)
            {
                _index++;
            }
        }

        public void Last()
        {
            _index = _carFile.Cars.Count - 1;
        }
    }
}
